/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：NewsInfoEdit.cs
* 创建标识：曾乃强 15158756652 2011-3-21 10:49:37
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class NewsInfoEdit : AdminPage
    {
        BLL.CNVP_NewsInfo bll = new BLL.CNVP_NewsInfo();
        Model.CNVP_NewsInfo model = new Model.CNVP_NewsInfo();
        public int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 99)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                ddlNewsClass();
                if (id > 0)
                {
                    lbTitle.Text = " > 修改";
                    DataInfo(id);
                }
                else
                {
                    int NewClassID = 0;
                    if (!string.IsNullOrEmpty(Request.QueryString["NewClassID"]) && Public.IsNumber(Request.QueryString["NewClassID"].ToString()))
                    {
                        NewClassID = Convert.ToInt32(Request.QueryString["NewClassID"]);
                    }
                    if (NewClassID > 0)
                    {
                        ddlNewsClassID.SelectedValue = NewClassID.ToString();
                    }
                    lbTitle.Text = " > 添加";
                }
                HotProList();
            }
           
        }

        private void HotProList()
        {
            string AddTr = "";
            AddTr += "<table id=\"tableEdit\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
            if (txtHotLink.Text.Trim() != "")
            {
                string[] resultArray = txtHotLink.Text.Trim().Split(Convert.ToChar(","));
                for (int i = 0; i < resultArray.Length; i++)
                {
                    Model.CNVP_Products model = new Model.CNVP_Products();
                    BLL.CNVP_Products bll = new BLL.CNVP_Products();
                    model = bll.GetSingle_CNVP_ProductsById(int.Parse(resultArray[i]));
                    if (model != null)
                    {
                        AddTr += "	<tr id=\"tr" + (i + 1) + "\">";
                        AddTr += "<td style=\"width=30px;\">";
                        AddTr += i + 1;
                        AddTr += "</td>";
                        AddTr += "		<td style=\"width:100px;\">";
                        AddTr += "         <a href=\"/ProductDetail_" + model.ProductClassID + "_" + model.ID + ".aspx\" target=\"_blank\" title=\"" + model.ProductName + "\">";
                        AddTr += "		      <img src=\"/UploadFile/Product/" + model.ImgProLoad + "\" class=\"BuyIsPrintPic\"> ";
                        AddTr += "         </a>";
                        AddTr += "      <input type=\"hidden\" id=\"ImgProLoad\" name=\"ImgProLoad\" value=\"" + model.ImgProLoad + "\" />";
                        AddTr += "		</td>";
                        AddTr += "		<td style=\"text-align:left;vertical-align:middle;\">";
                        AddTr += "         <a href=\"/ProductDetail_" + model.ProductClassID + "_" + model.ID + ".aspx\" target=\"_blank\" title=\"" + model.ProductName + "\">";
                        AddTr += "          " + model.ProductName + "";
                        AddTr += "          </a>";
                        AddTr += "      <input type=\"hidden\" id=\"ProductName\" name=\"ProductName\" value=\"" + model.ProductName + "\" />";
                        AddTr += "		</td>";

                        AddTr += "<td style=\"width:200px;vertical-align:middle;\">";

                        AddTr += "<a onclick=\"onMoveUp(this)\"  style=\"cursor:pointer\"> ";
                        AddTr += "向上";
                        AddTr += "</a>";
                        AddTr += "<a onclick=\"onMoveDown(this)\" style=\"cursor:pointer\"> ";
                        AddTr += "向下";
                        AddTr += "</a>";
                        AddTr += "</td>";

                        AddTr += "		<td style=\"width:20px;vertical-align:middle;\">";
                        AddTr += "		<input id=\"BTDel\" type=\"button\" value=\"删除\" onclick='Del(" + resultArray[i] + ")' />";
                        AddTr += "      <input type=\"hidden\" id=\"ProductID\" name=\"ProductID\" value=\"" + resultArray[i] + "\" />";
                        AddTr += "		</td>";
                        AddTr += "	</tr>";
                    }
                }
            }
            AddTr += "</table>";
            lbHotProList.Text = AddTr;
        }

        private void ddlNewsClass()
        {
            IList<Model.CNVP_Column> model = new BLL.CNVP_Column().GetAll_CNVP_Column("ColumnType = 2  and IsShow=1");
            foreach (Model.CNVP_Column c in model)
            {
                ddlNewsClassID.Items.Add(new ListItem(c.ColumnName,c.ID.ToString()));
            }
        }
        public void DataInfo(int id)
        {
            model = bll.GetSingle_CNVP_NewsInfoById(id);
            if (model != null)
            {
                ddlNewsClassID.SelectedValue = model.NewsClassID.ToString();
                txtNewsTitle.Text = model.NewsTitle;
                txtNewsContent.Value = model.NewsContent;
                rblIsShow.SelectedValue = model.IsShow.ToString();
                txtHotLink.Text = model.HotLink;
                ViewState["seoInId"] = model.SeoInfoID;
                //SEO优化
                var seoInfo = new BLL.CNVP_SEOInfo().GetSingle_CNVP_SEOInfoById(model.SeoInfoID);
                if (seoInfo != null)
                {
                    txtOriginTitle.Text = seoInfo.OriginTitle;
                    txtOriginKey.Text = seoInfo.OriginKey;
                    txtOriginDescription.Text = seoInfo.OriginDescription;
                }
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            if (id > 0)
            {
                int rows = new BLL.CNVP_NewsInfo().Update_CNVP_NewsInfo(
                       new Model.CNVP_NewsInfo()
                       {
                           ID = id,
                           NewsClassID = int.Parse(ddlNewsClassID.SelectedValue),
                           NewsTitle = txtNewsTitle.Text.Trim(),
                           NewsContent = txtNewsContent.Value.Trim(),
                           IsShow = byte.Parse(rblIsShow.SelectedValue),
                           ShowTime = DateTime.Now,
                           AddTime = DateTime.Now,
                           UpdateTime = DateTime.Now,
                           HotLink = txtHotLink.Text.Trim(),
                           SeoInfoID=int.Parse(ViewState["seoInId"].ToString())
                       }

               );
                if (rows == 1)
                {
                    int seoInfo = new BLL.CNVP_SEOInfo().Update_CNVP_SEOInfo(new Model.CNVP_SEOInfo()
                    {
                        ID = int.Parse(ViewState["seoInId"].ToString()),
                        OriginTitle = txtOriginTitle.Text.Trim(),
                        OriginKey = txtOriginKey.Text.Trim(),
                        OriginDescription = txtOriginDescription.Text.Trim(),
                    });
                    txtHotLink.Text = "";
                    BasePage.MessageBox("修改成功！", "NewsInfoList.aspx?Page=" + Request.QueryString["Page"] + "");
                    DataInfo(id);
                }
                else
                {
                    txtHotLink.Text = "";
                    BasePage.MessageBoxGoHistory("修改失败！");
                }
            }
            else
            {
                try
                {
                    int rows_SeoInfo = new BLL.CNVP_SEOInfo().Add_CNVP_SEOInfo(new Model.CNVP_SEOInfo()
                    {
                        OriginTitle = txtOriginTitle.Text.Trim(),
                        OriginKey = txtOriginKey.Text.Trim(),
                        OriginDescription = txtOriginDescription.Text.Trim(),
                    });
                    int rows = new BLL.CNVP_NewsInfo().Add_CNVP_NewsInfo(
                            new Model.CNVP_NewsInfo()
                            {
                                NewsClassID = int.Parse(ddlNewsClassID.SelectedValue),
                                NewsTitle = txtNewsTitle.Text.Trim(),
                                NewsContent = txtNewsContent.Value.Trim(),
                                IsShow = byte.Parse(rblIsShow.SelectedValue),
                                ShowTime = DateTime.Now,
                                AddTime = DateTime.Now,
                                HotLink = txtHotLink.Text.Trim(),
                                SeoInfoID = rows_SeoInfo,//最大值
                                UpdateTime = DateTime.Now
                            }

                    );
                    txtHotLink.Text = "";
                    BasePage.MessageBox("添加成功！", "NewsInfoList.aspx");
                }
                catch
                {
                    txtHotLink.Text = "";
                    BasePage.MessageBoxGoHistory("添加失败！"); 
                }
            }
        }
    }
}

